import 'package:flutter/material.dart';
import 'package:terminal_flutter_demo/widgets/my_image.dart';

// ignore: must_be_immutable
class MyDrawer extends StatefulWidget {
  List<Widget> widgets;

  void Function(Widget widget) onTap;

  MyDrawer({Key? key, required this.widgets, required this.onTap})
      : super(key: key);

  @override
  _MyDrawerState createState() => _MyDrawerState();
}

class _MyDrawerState extends State<MyDrawer> {
  var userAccountHeader = true;

  Widget _getDrawerHeader() {
    if (userAccountHeader) {
      return const UserAccountsDrawerHeader(
        decoration: BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.cover,
            image: NetworkImage(
                'https://picx.zhimg.com/v2-983cf0315c624aee23a9c34de7fb7708_250x0.jpg?source=172ae18b'),
          ),
        ),
        currentAccountPicture: MyCircleImage.url(
            'https://picx.zhimg.com/v2-3e2bfeaf16114e3a053c790950a4ac39_250x0.jpg?source=172ae18b'),
        accountName: Text('accountName'),
        accountEmail: Text('accountEmail'),
      );
    }
    return const DrawerHeader(
      decoration: BoxDecoration(
        image: DecorationImage(
          fit: BoxFit.cover,
          image: NetworkImage(
              'https://picx.zhimg.com/v2-983cf0315c624aee23a9c34de7fb7708_250x0.jpg?source=172ae18b'),
        ),
      ),
      child: SizedBox(
        child: Column(
          children: [
            MyCircleImage.url(
                'https://picx.zhimg.com/v2-3e2bfeaf16114e3a053c790950a4ac39_250x0.jpg?source=172ae18b'),
            ListTile(
              title: Text('title'),
              subtitle: Text('subtitle'),
            ),
          ],
        ),
      ),
    );
  }

  List<Widget> _getListData() {
    return widget.widgets
        .map((item) => (ListTile(
              leading: const Icon(Icons.comment),
              title: Text(item.toString()),
              onTap: () {
                // Navigator.of(context).pop();
                Navigator.pop(context);
                widget.onTap(item);
              },
            )))
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Column(
        children: [
          _getDrawerHeader(),
          Expanded(
            flex: 1,
            child: ListView(
              children: _getListData(),
            ),
          )
        ],
      ),
    );
  }
}
